<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20" class="el-row-table">
        <el-col :span="12"
          ><div class="grid-content bg-purple">
            <el-input placeholder="请输入内容" clearable>
              <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input></div
        ></el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="addParamDialogVisible = true"
              >新增系统参数</el-button
            >
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="success" @click="getParamList">刷新</el-button>
          </div>
        </el-col>
      </el-row>

      <!-- 系统参数列表区域 -->
      <el-table border stripe width="100%" :data="paramList">
        <el-table-column fixed label="序号" prop="id"> </el-table-column>
        <el-table-column fixed label="名称" prop="note"> </el-table-column>
        <el-table-column fixed label="关键字" prop="name"> </el-table-column>

        <el-table-column fixed label="值" prop="value"> </el-table-column>
        <!-- <el-table-column fixed label="创建者" width="100" prop="createUser">
        </el-table-column>
        <el-table-column fixed label="创建时间" width="250" prop="createTime">
        </el-table-column> -->

        <el-table-column fixed label="操作">
          <template slot-scope="scope">
            <!-- 第一个是查看详情按钮 -->
            <!-- <el-tooltip
              effect="dark"
              content="查看"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="success"
                icon="el-icon-folder-opened"
                circle
                @click="showDialog(scope.row.id)"
              ></el-button>
            </el-tooltip> -->

            <!-- 第二个删除按钮 -->
            <el-tooltip
              effect="dark"
              content="删除"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="removeDialog(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.current"
        :page-sizes="[5, 10]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 数据字典详情对话框 -->
    <el-dialog
      title=" 系统参数详情"
      :visible.sync="showParamDialogVisible"
      width="50%"
      center
    >
      <!-- 内容主体区 -->
      <el-form :model="showForm" label-width="100px">
        <el-form-item label="序号">
          <el-input v-model="showForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="showForm.note" disabled></el-input>
        </el-form-item>
        <el-form-item label="关键字">
          <el-input v-model="showForm.name" disabled></el-input>
        </el-form-item>

        <el-form-item label="值">
          <el-input v-model="showForm.value" disabled></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="showParamDialogVisible = false">关 闭</el-button>
        <!-- <el-button type="primary" @click="updateParameter">确认</el-button> -->
      </span>
    </el-dialog>

    <!-- 添加系统参数对话框 -->
    <el-dialog
      title="添加系统参数"
      :visible.sync="addParamDialogVisible"
      width="50%"
      @close="addParamDialogClosed"
      center
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addParamForm"
        :rules="addParamRules"
        ref="addParamRef"
        label-width="100px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="addParamForm.name"></el-input>
        </el-form-item>

        <el-form-item label="关键字" prop="note">
          <el-input v-model="addParamForm.note"></el-input>
        </el-form-item>

        <el-form-item label="值" prop="value">
          <el-input v-model="addParamForm.value"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addParamDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSavaParam">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 删除系统参数对话框 -->
    <el-dialog
      title="删除系统参数"
      :visible.sync="removeParamDialogVisible"
      width="30%"
      center
    >
      <span>确定删除掉:{{ removeParamForm.name }} 吗?</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="removeParamDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="removeParameter">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getParam,
  updateParam,
  pageParam,
  saveParam,
  removeParam,
  getParamByName,
} from "@/api/parameter.js";
export default {
  data() {
    const validateNote = (rule, value, callback) => {
      this.checkForm.forEach((param) => {
        if (param.note === value) {
          callback(new Error("该关键字已经存在"));
          return;
        }
      });
      callback();
    };
    return {
      //获取用户列表的参数对象
      queryInfo: {
        current: 1,
        size: 5,
        name: "",
      },
      total: 0,

      paramList: [],
      showParamDialogVisible: false,
      showForm: {},
      //添加系统参数弹窗的显示与隐藏
      addParamDialogVisible: false,
      //添加系统参数的表单
      addParamForm: {
        name: "",
        note: "",
        value: "",
      },
      //添加系统参数的系统规则
      addParamRules: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur",
          },
        ],
        note: [
          {
            required: true,
            message: "请输入关键字",
            trigger: "blur",
          },
          {
            validator: validateNote,
            trigger: "blur",
          },
        ],
        value: [
          {
            required: true,
            message: "请输入值",
            trigger: "blur",
          },
        ],
      },
      //用来存储已经存在的系统参数专门设置的一个数组
      checkForm: [],
      //删除弹窗的显示与隐藏
      removeParamDialogVisible: false,
      //保存删除对象
      removeParamForm: {},
    };
  },
  created() {
    this.getParamList();
  },
  methods: {
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize;
      this.getParamList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.current = newPage;
      this.getParamList();
    },
    //获得系统参数的列表
    async getParamList() {
      const { data: res } = await pageParam(
        this.queryInfo.current,
        this.queryInfo.size
      );
      this.paramList = res.data.records;

      //这个是数组的赋予
      this.paramList.forEach((param) => {
        this.checkForm.push({
          name: param.name,
          value: param.value,
          note: param.note,
        });
      });

      this.total = res.data.total;
    },

    //展示数据字典详情的对话框
    showDialog(id) {
      getParam(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.showParamDialogVisible = true;
          this.showForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //点击确定提交修改
    updateParameter() {
      updateParam(this.showForm.id, this.showForm.value)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.showParamDialogVisible = false;
          this.getParamList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //重置新增系统参数填写的窗口
    addParamDialogClosed() {
      this.$refs.addParamRef.resetFields();
    },
    //新增系统参数
    onSavaParam() {
      //下面这些等接口开放再调用
      saveParam(this.addParamForm)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.addParamDialogVisible = false;
          this.getParamList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //删除提示框展示
    removeDialog(id) {
      getParam(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.removeParamDialogVisible = true;
          this.removeParamForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    removeParameter() {
      var id = this.removeParamForm.id;
      removeParam(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.removeParamDialogVisible = false;
          this.getParamList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.breadcrumb {
  margin-bottom: 15px;
  font-size: 15px;
}
.box-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.el-row-table {
  margin-bottom: 15px !important;
}
.el-pagination {
  margin-top: 15px;
}
</style>